<?php
require_once('init.php');
$type_list = array("list","add","one");

$type = trim($_GET['type']);
if (!in_array($type, $type_list)) $type = "list";

$goods_mng = new MerchantGoods();

$html = <<<EOT
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
<title>商品信息</title>
<link rel="stylesheet" href="../common/style.css" type="text/css" />
</head>
<body>
EOT;

$cate_mng = new MerchantCate();
$cate_list = $cate_mng->getAll();
$cate_options = $cate_mng->getOptions();

switch ($type)
{
	case 'list':
		$html .= '<div><a href="../">首页</a>&nbsp;<a href="?type=add">添加商品</a></div>';

		$page = $_GET['page'] ? (int)$_GET['page'] : 0;
		$size = $_GET['size'] ? (int)$_GET['size'] : 20;
		$list = $goods_mng->getList($page, $size);
		if ($list) {
			$html .= '<table><thead>';
			$html .= '<th>商品ID</th>';
			$html .= '<th width="100px">商品名称</th>';
			$html .= '<th>所属分类</th>';
			$html .= '<th>单价</th>';
			$html .= '<th>商品总数</th>';
			$html .= '<th>库存</th>';
			$html .= '<th width="300px">描述信息</th>';
			$html .= '<th>下单操作</th>';
			$html .= '</thead>';
			foreach ($list as $row) {
				$html .= "<tr><td>".$row['id']."</td>";
				$html .= "<td><a href='?type=one&id=".$row['id']."'>".$row['name']."</a></td>";
				$html .= "<td>".$cate_list[$row['cid']]['name']."</td>";
				$html .= "<td>￥".$row['price']."</td>";
				$html .= "<td>".$row['total']."</td>";
				$html .= "<td>".$row['stock']."</td>";
				$html .= "<td>".$row['description']."</td>";
				$html .= "<td><form action='order.php' method='post'><input type='hidden' name='type' value='add' /><input type='hidden' name='Order[goods_id][]' value='".$row['id']."' />数量：<input type='text' name='Order[num][]' value='1' size='3' />&nbsp;<input type='submit' value='购买' /></form></td></tr>";
			}
			$html .= "</table>";
		} else {
			$html .= '<br/>没有商品信息！';
		}
		break;
	case 'add':
		$html .= '<div><a href="../">首页</a>&nbsp;<a href="?type=list">商品列表</a></div>';

		if ($_POST['Goods'])
		{
			$post = $_POST['Goods'];
			$insert_data = array(
				'id'=>(int)$post['pid'],
				'name'=>addslashes($post['name']),
				'cid'=>(int)$post['cid'],
				'price'=>(float)$post['price'],
				'total'=>(int)$post['total'],
				'stock'=>(int)$post['total'],
				'description'=>addslashes($post['description']),
				'modtime'=>date('Y-m-d H:i:s')
			);
			$ok = $goods_mng->insert($insert_data);
			header('Location:?type=list');
		}

		$html .= '<form action="" method="post"><table>';
		$html .= '<tr><td>名称：</td><td><input type="text" name="Goods[name]" /></td>';
		$html .= '<tr><td>所属分类：</td><td><select name="Goods[cid]"><option>请选择</option>';
		$html .= $cate_options;
		$html .= '</select></td>';
		$html .= '<tr><td>单价：</td><td><input type="text" name="Goods[price]" /></td>';
		$html .= '<tr><td>商品总数：</td><td><input type="text" name="Goods[total]" /></td>';
		$html .= '<tr><td>描述信息：</td><td><textarea name="Goods[description]" cols="60" rows="5"></textarea></td>';
		$html .= '<tr><td colspan=2><input type="submit" value="提交" /></td>';
		$html .= '</table></form>';
		break;
	case 'one':
		$goods_info = $goods_mng->getOne($_GET['id']);
		if (!$goods_info) {
			header('Location:?type=list');		
		}

		$html .= '<div><a href="../">首页</a>&nbsp;<a href="?type=list">商品列表</a></div>';

		$html .= '<table>';
		$html .= '<tr><td>名称：</td><td>'.$goods_info['name'].'</td>';
		$html .= '<tr><td>所属分类：</td><td>'.$cate_list[$goods_info['cid']]['name'].'</td>';
		$html .= '<tr><td>单价：</td><td>'.$goods_info['price'].'</td>';
		$html .= '<tr><td>商品总数：</td><td>'.$goods_info['total'].'</td>';
		$html .= '<tr><td>库存：</td><td>'.$goods_info['stock'].'</td>';
		$html .= '<tr><td>描述信息：</td><td>'.$goods_info['description'].'</td>';
		$html .= '</table>';
		$html .= "<form action='order.php' method='post'><input type='hidden' name='type' value='add' /><input type='hidden' name='Order[goods_id][]' value='".$goods_info['id']."' />数量：<input type='text' name='Order[num][]' value='1' size='3' />&nbsp;<input type='submit' value='购买' /></form>";

		break;
}

$html .= <<<EOT
</body>
</html>
EOT;

echo $html;
?>
